angular.module('furion.controllers.accounts', [])

.controller 'AccountCtrl', ($scope, $state, RemoteSrv, Toast) ->
  $scope.account =
    total_amount: '--'
    accumulated_income: '--'
    money: '--'
  $scope.clickCard = ()->
    result = furion.utils.checkCardCertify($scope.account)
    if !result.result
      Toast.show result.msg
      $state.go result.state, result.params
    else
      $state.go 'bankcard'
    return

  $scope.getAccountInfo = ()->
    $scope.account = furion.storage.account()
    RemoteSrv.getAccountInfo().done (resp)->
      furion.storage.updateAccount(resp.data.account)
      $scope.account = furion.storage.account()
    .always ()->
      $scope.$broadcast('scroll.refreshComplete')
    data = Mock.mock('https://www.juncaijinfu.com:9527/api', 'POST',
      'number1|1-100.1-10': 1,
      'number2|123.1-10': 1,
      'number3|123.3': 1,
      'number4|123.10': 1.123
    )
    console.log data
    return

  $scope.$on '$ionicView.enter', (event)->
    $scope.getAccountInfo()

  return

.controller 'AlbumsCtrl', ($scope, $ionicActionSheet, Albums)->
  # $scope.images = []
  $scope.shouldDelete = false
  $scope.shouldDeleteThis = null
  $scope.shouldDeleteValue = '整理'

  $scope.setOption = (srcType)->
    options = 
      quality: 50,
      destinationType: Camera.DestinationType.FILE_URL,
      sourceType: srcType,
      encodingType: Camera.EncodingType.JPEG,
      mediaType: Camera.MediaType.PICTURE,
      allowEdit: true,
      correctOrientation: true

  createNewFileEntry = (imgUri) ->
    window.resolveLocalFileSystemURL cordova.file.cacheDirectory, (dirEntry)->
      dirEntry.getFile "tempFile.jpeg", { create: true, exclusive: false }, (fileEntry)->
        console.log("got file: " + fileEntry.fullPath)

      , onErrorCreateFile ->
    , onErrorResolveUrl ->

  $scope.getP = (t)->
    if t == 'camera'
      srcType = Camera.PictureSourceType.CAMERA
    else if t == 'photo'
      # srcType = Camera.PictureSourceType.PHOTOLIBRARY,
      srcType = Camera.PictureSourceType.SAVEDPHOTOALBUM
    options = $scope.setOption(srcType)
    func = createNewFileEntry

    cameraSuccess = (ImgUrl)->
      console.log ImgUrl

    cameraError = (error)->
      console.debug 'Unable to obtain picture : ' + error + 'app'

    navigator.camera.getPicture(cameraSuccess, cameraError, options)

  $scope.addImg = (t)->
    return unless furion.utils.isDevice() # 网页内不进行操作    
    $scope.getP(t)

  $scope.choseAddImg = ()->
    hideSheet = $ionicActionSheet.show
      buttons: [
        {text: '相机拍摄'},
        {text: '从手机图库选择'}
      ],
      cancelText: '取消',
      buttonClicked: (index)->
        if index == 0
          $scope.addImg('camera')
          console.debug '拍摄'
        else if index == 1
          $scope.addImg('photo')
          console.debug '从图库选择'

        return true

  # 删除按钮
  $scope.showDelete = ()->
    if !$scope.shouldDelete
      $scope.shouldDelete = true
      $scope.shouldDeleteValue = '完成'
    else
      $scope.shouldDelete = false
      $scope.shouldDeleteValue = '整理'

  # 删除图片
  $scope.deleteImage = (index)->
    $scope.shouldDeleteThis = index
    hideSheet = $ionicActionSheet.show
      # buttons: [
      #   {text: '删除'}
      # ]
      destructiveText: '删除'
      titleText: '确定要删除此图片吗?'
      cancelText: '取消'
      cancel: ()->
        $scope.shouldDeleteThis = null
      destructiveButtonClicked: ()->
        $scope.images.splice(index, 1)
        $scope.shouldDeleteThis = null
        return true
      # buttonClicked: ()->
      #   $scope.images.splice(index, 1)
      #   console.log index
      #   return true

  # 打开图片modal
  $scope.openImgModal = (url, index)->
    return unless !$scope.shouldDelete
    $scope.$emit 'modals:open', {modal_name: 'image_modal', params: {url: url}}

  $scope.getAlbums = ()->
    $scope.images = Albums.all
    $scope.$broadcast 'scroll.refreshComplete'
    $scope.$broadcast 'scroll.infiniteScrollComplete'

  $scope.$on '$ionicView.enter', (event)->
    if !$scope.images
      $scope.images = Albums.all

  return

.controller 'RecordsCtrl', ($scope, MyRecords)->
  $scope.records = MyRecords.all
  # $scope.shouldShowDelete = false
  # $scope.toggleDelete = ()->
  #   if $scope.shouldShowDelete == false
  #     $scope.shouldShowDelete = true
  #   else
  #     $scope.shouldShowDelete = false

  $scope.remove =(item)->
    console.log item
    $scope.records.splice($scope.records.indexOf(item), 1)

  $scope.edit = (item)->
    $scope.$emit 'modals:open', {modal_name: 'edit_modal', params: item}

  return

.controller 'FavoriteCtrl', ($scope)->

  return

.controller 'RemindCtrl', ($scope)->

  return

.controller 'SettingsCtrl', ($scope, $state, Toast, RemoteSrv, $rootScope) ->
  $scope.$on '$ionicView.enter', (event)->
    $scope.is_login = furion.storage.isLogin()
    $scope.is_ios = ionic.Platform.isIOS()
    $scope.getAccountInfo()

  $scope.openGetPass = ()->
    $scope.$emit "modals:open", {modal_name: "get_pass"}
  $scope.openConfirm = ()->
    $scope.$emit "modals:open", {modal_name: "signoutConfirm"}
  $rootScope.$on "Signout",->
    $scope.signout()
  $scope.signout = () ->
    $scope.$emit "loading:show"
    RemoteSrv.signout().always( ()->
      $scope.$emit "loading:hide"
    )
    furion.storage.deleteAccount()
    $scope.is_login = false
    $state.go 'app.home'
    return

  $scope.clickAuth = ()->
    return if $scope.current_account.authenticated
    result = furion.utils.checkCardCertify($scope.current_account)
    if !result.result
      Toast.show result.msg
      $state.go result.state, result.params
      event.preventDefault()

  $scope.signin = () ->
    $scope.$emit "modals:open", {modal_name: 'signin'}
    return

  $scope.getAccountInfo = () ->
    return if !$scope.is_login
    params =
      ios_token: furion.storage.deviceInfo().ios_token
      jpush_token: furion.storage.deviceInfo().jpush_token
    RemoteSrv.getAccountInfo(params).done (resp)->
      furion.storage.updateAccount(resp.data.account)
      $scope.current_account = furion.storage.account()
    .always ->
      # $scope.$broadcast 'scroll.refreshComplete'
      # $scope.$broadcast 'scroll.infiniteScrollComplete'
  return
# .controller 'SettingsCtrl'